﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerUI;
using System.IO;

namespace orz.mouxianyu.Extensions
{
    public static class SwaggerServiceExtensions
    {
        public static IServiceCollection AddSwaggerDocumentation(this IServiceCollection services)
        {
            services.AddSwaggerGen(c =>
            {

                // Swagger 2.+ support
                var security = new Dictionary<string, IEnumerable<string>>
                {
                    {"Bearer", new string[] { }},
                };
                c.AddSecurityRequirement(security);

                c.SwaggerDoc("v1", new Info { Title = "咸鱼框架v1.0测试版", Version = "v1" });
                //c.OperationFilter<SwaggerFileUploadFilter>();//增加文件过滤处理
                //var basePath = AppContext.BaseDirectory;
                var basePath1 = AppDomain.CurrentDomain.BaseDirectory;
                var assemblyName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
                //var xmlApiPath = Path.Combine(basePath, "Chaunce.Api.xml");//api文件xml(在以上步骤2设置生成xml的路径)

                var fileName = System.IO.Path.GetFileName(assemblyName + ".xml");
                var xmlApiPath = Path.Combine(basePath1, fileName);

                c.IncludeXmlComments(xmlApiPath);

                //c.OperationFilter<SwaggerFileUploadFilter>();
                ////options.OperationFilter<SwaggerTagAttribute>();
                //c.OperationFilter<AuthorizeCheckOperationFilter>();
                c.DocumentFilter<ApplyTagDescriptions>();

                c.IgnoreObsoleteActions();
                c.AddSecurityDefinition("Bearer", new ApiKeyScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                    Name = "Authorization",
                    In = "header",
                    Type = "apiKey"
                });//Authorization的设置

            });

            return services;
        }

        public static IApplicationBuilder UseSwaggerDocumentation(this IApplicationBuilder app)
        {
            app.UseSwagger();
            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
            //app.UseSwagger(c =>
            //{
            //    //c.RouteTemplate = "docs/{documentName}/docs.json";//使中间件服务生成Swagger作为JSON端点(此处设置是生成接口文档信息，可以理解为老技术中的webservice的soap协议的信息，暴露出接口信息的地方)
            //    c.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Info.Description = httpReq.Path);//请求过滤处理
            //});
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1");

                //c.DocumentTitle = "Title Documentation";
                //c.DocExpansion(DocExpansion.None);
            });

            return app;
        }
    }
}
